<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/8/1
 * Time: 13:24
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\admin\controller;

use app\common\business\SpecsValue as BusObj;
use app\common\lib\Show;
use app\common\lib\Status;
use think\App;
use think\facade\View;

class SpecsValue extends AdminBase
{
    protected ?BusObj $busObj = null;

    public function __construct(App $app)
    {
        $this->busObj = new BusObj();
        parent::__construct($app);
    }

    /**
     * @return \think\response\Json
     */
    public function getBySpecsId(): \think\response\Json
    {
        $specs_id = $this->request->param('specs_id', 0, 'intval');
        try {
            $result = $this->busObj->getBySpecsId($specs_id);
        } catch (\Exception $e) {
            return Show::json_show(Status::Error, $e->getMessage());
        }
        return Show::json_show(Status::Success, "ok", $result);
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->param(null, null, 'trim');

            try {
                $result = $this->busObj->addById($data);
            } catch (\Exception $e) {
                return Show::json_show(Status::Error, $e->getMessage());
            }
            if ($result) {
                return Show::json_show(Status::Success, "操作成功", ["id" => $result]);
            } else {
                return Show::json_show(Status::Error, "操作失败");
            }
        }
        return View::fetch();
    }
}